package com.yiren.dbaa.module.project_edit.view.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yiren.backstage.config.enums.ExamUiEnum;
import com.yiren.backstage.config.enums.PlayOrderEnum;
import com.yiren.backstage.config.enums.SiteConfigCodeEnum;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.model.ScreenModel;
import com.yiren.dbaa.module.project_edit.contract.SiteInfoSetContract;
import com.yiren.dbaa.module.project_edit.presenter.SiteInfoSetPresenter;
import com.yiren.dbaa.module.project_edit.service.IDataOperation;
import com.yiren.dbaa.module.project_edit.service.impl.DataOperationImpl;
import com.yiren.dbaa.module.project_edit.view.adapter.ChoiceProjectAdapter;
import com.yiren.dbaa.module.project_edit.view.adapter.ExamTemplateAdapter;
import com.yiren.dbaa.module.project_edit.view.adapter.RandomLineAdapter;
import com.yiren.dbaa.module.project_edit.view.adapter.SelectParamAdapter;
import com.yiren.dbaa.util.DensityUtil;
import com.yiren.dbaa.util.GraphicUtil;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Describe 考规--练车设置对话框
 *
 * @author WRX
 * @date 2023/1/11
 */
public class SiteInfoDialog {

    private Activity activity;
    private SiteInfoSetContract.Presenter presenter;
    private IDataOperation iDataOperation;

    public SiteInfoDialog(Activity activity) {
        this.activity = activity;
        presenter = new SiteInfoSetPresenter(activity);
        iDataOperation = new DataOperationImpl();
    }

    /**
     * 项目选择弹出框
     *
     * @param type    1:靠边停车 2：执行一次
     * @param tempMap 存放参数值改变的临时map
     */
    public void showSelectProjectDialog(int type, Map<String, Object> tempMap) {
        //*** 可选的项目 */
        List<ProjectEntity> choiceProjects = presenter.getChoiceProjects(type);
        if (choiceProjects.size() == 0) {
            MessageBox.MessageAndVoiceModel(activity, activity.getString(R.string.wfa_point_list_is_null_message));
        } else {
            // 已选的项目id集合 */
            List<String> selectedProjects;
            if (type == 1) {
                selectedProjects = (List<String>) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L8.toString());
            } else {
                selectedProjects = (List<String>) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L10.toString());
            }
            Dialog dialog = new AlertDialog.Builder(activity).create();
            dialog.show();
            WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
            ScreenModel screen = GraphicUtil.getResolutionInfo(activity);
            params.width = (screen.getWidth() * 7) / 10;
            params.height = (screen.getHeight() * 6) / 10;
            dialog.getWindow().setAttributes(params);
            Window window = dialog.getWindow();
            window.setContentView(R.layout.dialog_select_box);
            TextView tvTitle = (TextView) window.findViewById(R.id.tv_title);
            ListView lvProject = (ListView) window.findViewById(R.id.lv_dialog_list);
            LinearLayout llBtn = (LinearLayout) window.findViewById(R.id.ll_button);
            TextView tvOk = (TextView) window.findViewById(R.id.tv_ok);
            TextView tvSplitLine = (TextView) window.findViewById(R.id.split_line);
            TextView tvCancel = (TextView) window.findViewById(R.id.tv_cancel);
            tvTitle.setText(type == 1 ? R.string.select_kbtc_project : R.string.select_exe_one_project);
            lvProject.setDivider(null);
            ChoiceProjectAdapter adapter = new ChoiceProjectAdapter(activity, choiceProjects, selectedProjects);
            lvProject.setAdapter(adapter);
            llBtn.setVisibility(View.VISIBLE);
            tvSplitLine.setVisibility(View.GONE);
            tvCancel.setVisibility(View.GONE);
            // 上面的布局是公用的，所以这里多余的控件要隐藏掉
            tvOk.setOnClickListener(v -> {
                presenter.filterDeleteProject(selectedProjects, choiceProjects);
                dialog.dismiss();
            });
            lvProject.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (selectedProjects.contains(choiceProjects.get(position).getId())) {
                        selectedProjects.remove(choiceProjects.get(position).getId());
                    } else {
                        selectedProjects.add(choiceProjects.get(position).getId());
                    }
                    if (type == 1) {
                        SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.L8.toString(), selectedProjects);
                        tempMap.put(SiteConfigCodeEnum.L8.toString(), selectedProjects);
                    } else {
                        SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.L10.toString(), selectedProjects);
                        tempMap.put(SiteConfigCodeEnum.L10.toString(), selectedProjects);
                    }
                    adapter.notifyDataSetChanged();
                }
            });
        }
    }

    /**
     * 选择随机播报线路弹框
     *
     * @param tempMap 存放参数值改变的临时map
     */
    public void showSelectRandomLine(List<LineInfoEntity> lines, Map<String, Object> tempMap) {
        if (null == lines || lines.size() == 0) {
            MessageBox.MessageAndVoiceModel(activity, activity.getString(R.string.wfa_line_list_is_null_message));
        } else {
            // 已选的线路id集合 */
            List<String> selectedLines = (List<String>) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L28.toString());
            Dialog dialog = new AlertDialog.Builder(activity).create();
            dialog.show();
            WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
            ScreenModel screen = GraphicUtil.getResolutionInfo(activity);
            params.width = (screen.getWidth() * 7) / 10;
            params.height = (screen.getHeight() * 6) / 10;
            dialog.getWindow().setAttributes(params);
            Window window = dialog.getWindow();
            window.setContentView(R.layout.dialog_select_box);
            TextView tvTitle = (TextView) window.findViewById(R.id.tv_title);
            ListView lvProject = (ListView) window.findViewById(R.id.lv_dialog_list);
            LinearLayout llBtn = (LinearLayout) window.findViewById(R.id.ll_button);
            TextView tvOk = (TextView) window.findViewById(R.id.tv_ok);
            TextView tvSplitLine = (TextView) window.findViewById(R.id.split_line);
            TextView tvCancel = (TextView) window.findViewById(R.id.tv_cancel);
            tvTitle.setText(R.string.open_random_play_line_tsy);
            lvProject.setDivider(null);
            RandomLineAdapter adapter = new RandomLineAdapter(activity, lines, selectedLines);
            lvProject.setAdapter(adapter);
            llBtn.setVisibility(View.VISIBLE);
            tvSplitLine.setVisibility(View.GONE);
            tvCancel.setVisibility(View.GONE);
            // 上面的布局是公用的，所以这里多余的控件要隐藏掉
            tvOk.setOnClickListener(v -> {
                presenter.filterDeleteLine(selectedLines, lines);
                dialog.dismiss();
            });
            lvProject.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (selectedLines.contains(lines.get(position).getId())) {
                        selectedLines.remove(lines.get(position).getId());
                    } else {
                        selectedLines.add(lines.get(position).getId());
                    }
                    SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.L28.toString(), selectedLines);
                    tempMap.put(SiteConfigCodeEnum.L28.toString(), selectedLines);
                    adapter.notifyDataSetChanged();
                }
            });
        }
    }

    /**
     * 显示考场模板选择弹框
     *
     * @param tempMap 存放参数值改变的临时map
     */
    public void showExamTemplateDialog(TextView tv, Map<String, Object> tempMap) {
        Dialog dialog = new AlertDialog.Builder(activity).create();
        dialog.show();
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = (DensityUtil.getScreenWidth(activity) * 7) / 10;
        params.height = (DensityUtil.getScreenHeight(activity) * 6) / 10;
        dialog.getWindow().setAttributes(params);
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_select_box);
        TextView tvTitle = (TextView) window.findViewById(R.id.tv_title);
        ListView lvList = (ListView) window.findViewById(R.id.lv_dialog_list);
        lvList.setDivider(null);// 去除listview的下划线
        tvTitle.setText(R.string.param_exam_ui);
        ExamTemplateAdapter adapter = new ExamTemplateAdapter(activity, new ExamTemplateAdapter.OnExamTempSelectListener() {
            @Override
            public void onExamTempSelect(int position) {
                ExamUiEnum[] examUis = ExamUiEnum.values();
                ExamUiEnum examUi = ExamUiEnum.valueOf((String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L1.toString()));
                if (!examUi.equals(examUis[position])) {
                    SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.L1.toString(), examUis[position].toString());
                    tempMap.put(SiteConfigCodeEnum.L1.toString(), examUis[position].toString());
                } else {
                    tempMap.remove(SiteConfigCodeEnum.L1.toString());
                }
                tv.setText(examUis[position].getDescription());
            }
        });
        lvList.setAdapter(adapter);
    }

    /**
     * 弹出框选择
     *
     * @param type    1:上车准备后接  2：模拟灯光后接 3：播报顺序
     * @param tempMap 存放参数值改变的临时map
     */
    public void showSelectDialog(int type, TextView tv, Map<String, Object> tempMap) {
        List<String> selectList = new ArrayList<>();
        List<ProjectEntity> selectProject = new ArrayList<>();
        if (type == 1) {
            selectProject.add(new ProjectEntity("0", "不执行"));
            selectProject.add(new ProjectEntity("41700", "模拟灯光"));
        } else if (type == 2) {
            selectProject.add(new ProjectEntity("0", "不执行"));
            List<ProjectEntity> projectList = iDataOperation.getProjectList();
            if (null != projectList && projectList.size() > 0) {
                selectProject.addAll(projectList);
            }
        }
        if (type == 1 || type == 2) {
            for (ProjectEntity project : selectProject) {
                selectList.add(project.getName());
            }
        } else {
            PlayOrderEnum[] playOrders = PlayOrderEnum.values();
            for (int i = 0; i < playOrders.length; i++) {
                selectList.add(playOrders[i].getDescription());
            }
        }
        Dialog dialog = new AlertDialog.Builder(activity).create();
        dialog.show();
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = (DensityUtil.getScreenWidth(activity) * 7) / 10;
        params.height = (DensityUtil.getScreenHeight(activity) * 6) / 10;
        dialog.getWindow().setAttributes(params);
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_select_box);
        TextView tvTitle = (TextView) window.findViewById(R.id.tv_title);
        ListView lvList = (ListView) window.findViewById(R.id.lv_dialog_list);
        lvList.setDivider(null);// 去除listview的下划线
        switch (type) {
            case 1:
            case 2:
                tvTitle.setText(R.string.select_exe_project);
                break;
            case 3:
                tvTitle.setText(R.string.param_play_order);
                break;
        }
        SelectParamAdapter adapter = new SelectParamAdapter(activity, selectList, tv.getText().toString().trim());
        lvList.setAdapter(adapter);
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv.setText(selectList.get(position));
                switch (type) {
                    case 1:  //上车准备后接项目
                        String sczbNextProject = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L2.toString());
                        if (!selectProject.get(position).getId().equals(sczbNextProject)) {
                            SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.L2.toString(), selectProject.get(position).getId());
                            tempMap.put(SiteConfigCodeEnum.L2.toString(), selectProject.get(position).getId());
                        } else {
                            tempMap.remove(SiteConfigCodeEnum.L2.toString());
                        }
                        break;
                    case 2: //模拟灯光后接项目
                        String mndgNextProject = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L3.toString());
                        if (!selectProject.get(position).getId().equals(mndgNextProject)) {
                            SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.L3.toString(), selectProject.get(position).getId());
                            tempMap.put(SiteConfigCodeEnum.L3.toString(), selectProject.get(position).getId());
                        } else {
                            tempMap.remove(SiteConfigCodeEnum.L3.toString());
                        }
                        break;
                    case 3: //播报顺序
                        PlayOrderEnum[] playOrders = PlayOrderEnum.values();
                        PlayOrderEnum playOrder = PlayOrderEnum.valueOf((String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L19.toString()));
                        if (!playOrder.equals(playOrders[position])) {
                            SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.L19.toString(), playOrders[position].toString());
                            tempMap.put(SiteConfigCodeEnum.L19.toString(), playOrders[position].toString());
                        } else {
                            tempMap.remove(SiteConfigCodeEnum.L19.toString());
                        }
                        break;
                }
                dialog.dismiss();
            }
        });
    }
}
